<!--
  - Copyright (c) 2019.  武汉中科图灵科技有限公司
  - Date :  2019/1/5
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.6.2
  -->

<template>
  <div class="calendar-list-container">
    <el-container>
      <el-container>
        <div class="flex-left-pannel">
          <el-row>
            <div class="xf-tabletop-handles">
              <div class="xf-tabhd-left">
                <el-button class="filter-item" @click="handleCreate" type="primary" size="mini">添加路线</el-button>
              </div>
              <div class="xf-tabhd-right">
                路线名
                <el-input v-model="listQuery.routeName" placeholder="请输入路线名" style="margin-right: 10px;" class="xf-query-input" clearable size="mini"></el-input>
                <el-button @click="tableQuery" type="primary" size="mini">查询</el-button>
              </div>
            </div>
          </el-row>
          <el-table
            key='tableKey'
            :data="list"
            ref="multipleTable"
            v-loading.body="listLoading" fit style="width: 100%"
            @selection-change="handleSelectionChange">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column label="路线名">
              <template slot-scope="scope">
                <span>{{scope.row.routeName}}</span>
              </template>
            </el-table-column>
            <el-table-column label="巡检设施">
              <template slot-scope="scope">
                <span>{{scope.row.labelCount}}个</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="130" fixed="right">
              <template slot-scope="scope">
                <span @click="handleUpdate(scope.row)" class="comm-table-btn"><icon-svg icon-class="bianji" style="fill:#50E3C2" /></span>
                <span @click="createDel(scope.row)" class="comm-table-btn"><icon-svg icon-class="shanchu" style="fill:#50E3C2" /></span>
              </template>
            </el-table-column>
          </el-table>
          <div v-show="!listLoading" class="sensor-pagination-container">
            <div>
              <el-checkbox v-model="isselall" :indeterminate="isIndeterminate" @change="handleCheckAllChange" v-show="list&&list.length!=0">全选</el-checkbox>
              <el-button type="text" style="margin-left: 10px;" @click="delAllSensor" v-show="list&&list.length!=0" :disabled="checklist&&checklist.length==0">全部删除</el-button>
            </div>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,15,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
          </div>
        </div>
      </el-container>
    </el-container>
    <el-dialog :visible.sync="isaddsensor" :show-close='false' custom-class="routedialog">
      <el-form :model="routeForm" ref="routeForm" :rules="routerules" label-width="100px" label-position='top'>
        <el-form-item label="路线名" prop="routeName">
          <el-input v-model.trim="routeForm.routeName" placeholder="请输入路线名" @keyup.enter.native="saveSensor('routeForm')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="dialogStatus==='create'" type="primary" @click="saveSensor('routeForm')" size="mini">确 定</el-button>
        <el-button @click="cancelAddSensor('routeForm')" type="text">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="删除巡检路线" :visible.sync="isdelsensor" :show-close='false' custom-class="xf-jc-dialog" width="360px" height="160px">
      <div class="xf-jc-deletect">
        <p v-if="iscanDelItem.length<1">确定删除巡检路线吗？</p>
        <p v-else>{{iscanDelItem.join(",")}}巡检路线已绑定巡检计划，请与巡检计划解绑后再进行删除</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" size="mini" @click="handleDelete" v-if="iscanDelItem.length<1">删除</el-button>
        <el-button type="primary" size="mini" @click="goUnbindSch" v-else>去解绑</el-button>
        <el-button size="mini" type="text" @click="isdelsensor=false">取消</el-button>
      </div>
    </el-dialog>
    <!--室内路线选取-->
    <insideHandle :Show.sync="insideHandleShow" :id.sync="routeId" :routeStName.sync="routeStatuName" :routeName.sync="syrouteName" @updateList="getList"></insideHandle>
  </div>
</template>
<script>
  import {
    pageInside,
    insideAddQuery,
    deleteListRoutes,
    deleteListRoutesQuery
  } from "@/api/device/inspectManager/index";

  import insideHandle from './insiderHandle';

  export default {
    name: "isideRoute",
    components:{insideHandle},
    data() {
      return {
        delRoutename:'',
        iscanDelItem:[],
        insideHandleShow:false,
        routeStatuName:'',
        curChannelId:'',
        routeId:'',
        visible2:false,
        isdelsensor:false,
        isselall:false,
        isIndeterminate:false,
        checklist:[],
        sensorid:"",
        channelId:"",
        syrouteName:'',
        list: [],
        total: null,
        listLoading: false,
        listQuery: {
          page: 1,
          limit: 15,
          routeName: '',
        },
        dialogStatus: "",
        tableKey: 0,
        isaddsensor:false,
        delsensorid:[],
        routeForm:{
          routeName:''
        },
        routerules: {
          routeName:[ {
            required: true,
            message: "请输入路线名",
            trigger: "blur"
          },
            {
              min: 1,
              max: 50,
              message: "最多允许输入50个字符",
              trigger: "blur"
            }]
        },
      };
    },
    created() {

    },
    methods: {
      fetchData(){
        this.handlerest();
        this.resetlistquery();
        //  路由变化重载数据
        this.curChannelId = this.$route.params.id;
        this.initLoad();
      },
      resetlistquery(){
        this.listQuery.page = 1;
        this.listQuery.limit =  15;
        this.listQuery.routeName =  '';
      },
      tableQuery() {
        this.getList();
      },
      handleCreate() {
        this.dialogStatus = "create";
        this.isaddsensor = true;
      },
      cancelAddSensor(formName) {
        const set = this.$refs;
        set[formName].resetFields();
        this.handlerest();
        this.isaddsensor = false;
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      initLoad() {
        this.getList();
      },
      getList() {
        this.listLoading = true;
        this.isselall=false;
        this.isIndeterminate=false;
        pageInside(Object.assign(this.listQuery,{channelId:this.curChannelId})).then(response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        })
      },
      handlerest(){
        this.routeForm.routeName=''
      },
      saveSensor(formName){
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            insideAddQuery(this.routeForm).then((res) => {
              if (res.status===200){
                this.syrouteName = this.routeForm.routeName;
                set[formName].resetFields();
                this.handlerest();
                this.getList();
                this.isaddsensor = false;
              //打开组件弹窗
                this.routeId = String(res.data.id);

                this.insideHandleShow = true;
                this.routeStatuName = "添加室内巡检路线";
              }
            });
          } else {
            return false;
          }
        });
      },
      handleUpdate(row){
        this.routeId = String(row.id);
        this.syrouteName = row.routeName;
        this.insideHandleShow = true;
        this.routeStatuName = "编辑室内巡检路线";
      },
      createDel(row){
        this.delsensorid=[];
        this.delsensorid.push(row.id);
        deleteListRoutesQuery({ids:(this.delsensorid).toString()}).then(res=>{
          this.iscanDelItem = res.data;
          this.isdelsensor=true;
        });
      },
      goUnbindSch(){
        this.$router.push('/inspectionManagement/patrolSchemeManager/'+this.curChannelId)
      },
      handleDelete(){
        deleteListRoutes({ids:(this.delsensorid).toString()}).then(res=>{
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.isdelsensor=false;
          this.listQuery.page = 1;
          this.getList();
        })
      },
      handleSelectionChange(val){
        this.checklist=[...val];
        let length = this.total>this.list.length?this.list.length:this.total;
        if (val.length===length) {
          this.isselall=true;
          this.isIndeterminate=false;
        }else{
          this.isselall=false;
          this.isIndeterminate=true;
        }
        if (val.length===0) {
          this.isIndeterminate=false;
        }
      },
      delAllSensor(){
        this.delsensorid=[];
        this.checklist.forEach((item,index)=>{
          this.delsensorid.push(item.id);
        });
        deleteListRoutesQuery({ids:(this.delsensorid).toString()}).then(res=>{
          this.iscanDelItem = res.data;
          this.isdelsensor=true;
        });
      },
      handleCheckAllChange(){
        this.$refs.multipleTable.toggleAllSelection();
      },
    },
    mounted() {
      this.fetchData();
    },
    watch:{
      '$route':'fetchData'
    },
  };
</script>
<style rel="stylesheet/scss" lang="scss">
  .xf-jc-deletect{
    p{
      word-break: break-all;
    }
  }
  .el-tree-node__children{
    i{
      &.itemstatus{
        vertical-align: middle;
        margin-right: 5px;
        width: 6px;
        height: 6px;
        display: inline-block;
        border-radius: 100%;
      }
      &.status0{
        background: #ffab22;
      }
      &.status1{
        background: #ff0000;
      }
      &.status2{
        background: #67A91B;
      }
    }
  }
  .flex-right-pannel_withline {
    border-left: 1px solid transparent;
    min-height: 86vh;
    box-sizing: border-box;
    padding-left: 10px;
    padding-top: 10px;
    .el-scrollbar__wrap {
      overflow-x: hidden;
    }
    .el-tree-node__label {
      font-size: 14px;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
  .el-table th {
    background: #f5f7fa;
  }
  .xf-query-input,
  .xf-query-select {
    width: 200px;
    margin: 0 10px;
  }
  .routedialog {
    width: 400px;
    .dialog-footer {
      text-align: left;
    }
    .input-with-select .el-input-group__append {
      background-color: #fff;
    }
  }
  // 精准查询
  .xf-double-form{
    .xf-dbform-item{
      display: flex;
      justify-content: space-between;
      .xf-dbform-item__sleft{
        .el-input{
          width: 320px;
        }
      }
      .xf-dbform-item__sright{
        .el-input{
          width: 320px;
        }
      }
    }
  }
  .upload-cum{
    .el-dialog__body{
      padding-top: 0;
      min-height: 40px;
      padding-bottom: 10px;
    }
    .dialog-footer{
      text-align: left;
    }
    .el-button.is-disabled{
      background-color: #d9d9d9;
      color: #fff;
      border-color: #d9d9d9;
    }
    .upload-wrap{
      width: 100%;
      margin-top:20px;
      .el-upload{
        width:100%;
      }
    }
    .el-upload-list__item{
      padding: 8px 0;
      background: #f1f1f1;
      .el-icon-close{
        top:14px;
      }
      .el-icon-upload-success{
        vertical-align: bottom;
      }
    }
    .el-upload-dragger{
      height: 40px;
    }
    .filedisabled .el-upload{
      display: none;
    }
    .el-button--default{
      border-color: #dcdfe6;
    }
    .uploadarea{
      width: 100%;
      line-height: 16px;
      background-color: #f1f1f1;
      border-radius: 2px;
      font-size: 14px;
      color: #666;
    }
    .el-upload-list__item .el-progress{
      top: 30px;
    }
  }
  .sensor-pagination-container{
    display:flex;
    justify-content:space-between;
    padding:10px 10px;
  }
  .build-custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>
